﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Net;
using Protocol;

namespace MDS
{
    public class FutureQuotationManager : QuotationManagerBase
    {

        public FutureQuotationManager(IQuotationProvider quotationProvider,
            ITradeDaysProvider tradeDaysProvider,
            IMessageRepository messageRepository,
            ISnapshotRepository<Protocol.GWMKTBasicInfoANS> basicSnapshotRepository,
            ISnapshotRepository<Protocol.GWMKTBidAskANS> bidSnapshotRepository,
            ISnapshotRepository<Protocol.GWMKTTicksANS> tickSnapshotRepository,
            HTSServer htsServer)
            : base(quotationProvider, tradeDaysProvider, messageRepository, basicSnapshotRepository, bidSnapshotRepository, tickSnapshotRepository, htsServer)
        {

        }

       protected override void _QuotationProvider_OnMessagesReceived(IEnumerable<IMessage> messages)
        {
            if (messages == null)
                return;
            var basicInfos = from msg in messages
                             where msg is GWMKTBasicInfoANS
                             select msg as GWMKTBasicInfoANS;
            var bids = from msg in messages
                       where msg is GWMKTBidAskANS
                       select msg as GWMKTBidAskANS;
            var ticks = from msg in messages
                        where msg is GWMKTTicksANS
                        select msg as GWMKTTicksANS;

            updateBasicInfoSnapshot(basicInfos);

            IEnumerable<GWMKTBasicInfoANS> sortedBasicInfos = getSortedBasicInfosFromSnapshot(basicInfos);
            enqueueBasicInfos(sortedBasicInfos);

            this._newBasicInfoReceivedEvent.Set();

            saveBidMsgs(bids);
            enqueueBids(bids);
            updateBidSnapshot(bids);


            saveTickMsgs(ticks);
            enqueueTicks(ticks);
            updateTickSnapshot(ticks);

            this._newMsgReceivedEvent.Set();

        }

        private IEnumerable<GWMKTBasicInfoANS> getSortedBasicInfosFromSnapshot(IEnumerable<GWMKTBasicInfoANS> updatedBasicInfos)
        {

            List<GWMKTBasicInfoANS> rtn = new List<GWMKTBasicInfoANS>();

            IEnumerable<GWMKTBasicInfoANS> allBasicInfos;
            this._basicSnapshotRepository.LoadSnapshot(out allBasicInfos);
            if (allBasicInfos != null)
            {
                var exchanges = (from bi in updatedBasicInfos
                                 select bi.ExchIDEx).Distinct();
                foreach (var exch in exchanges)
                {
                    foreach (var futureType in FutureType.AllFutureTypes)
                    {
                        bool existFutureType = updatedBasicInfos.Any(bi => bi.ExchIDEx == exch && bi.StkID.Value.Contains(futureType));

                        if (existFutureType)
                        {
                            var sortedFutureBasicInfos = from bi in allBasicInfos
                                                         where bi.ExchIDEx == exch && bi.StkID.Value.Contains(futureType)
                                                         orderby bi.EndDate
                                                         select bi;
                            int contractSerialNo = 1;
                            foreach (var bi in sortedFutureBasicInfos)
                            {
                                bi.ContractSerialNo = contractSerialNo++;
                                rtn.Add(bi);
                            }

                        }
                    }

                }

            }
            return rtn;
        }



    }
}
